﻿using DongFangGuoKai.WMSPlatform.WebApi.Middleware;

namespace DongFangGuoKai.WMSPlatform.WebApi.Extensions
{
    public static class WebApplicationExtension
    {
        public static void InitEnter(this WebApplication app)
        {
            // Configure the HTTP request pipeline.
            app.UseCors("MyAllowSpecificOrigins");

            if (app.Environment.IsDevelopment())
            {

            }
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                foreach (string version in typeof(ApiVersions).GetEnumNames())
                {
                    c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"东方国际WarehouseManagementSystem第【{version}】版本");
                }
            });

            app.UseAuthentication();

            app.UseAuthorization();


            // 启用WebSockets
            var socketAllowOrigins = app.Configuration.GetSection("SocketUrls").Get<List<string>>();
            var webSocketOptions = new WebSocketOptions()
            {
                //KeepAliveInterval - 向客户端发送“ping”帧的频率，以确保代理保持连接处于打开状态
                // 默认值为 2 分钟。
                KeepAliveInterval = TimeSpan.FromMinutes(2)
             
            };
            if (socketAllowOrigins != null && socketAllowOrigins.Count > 0)
            {
                //webSocketOptions.AllowedOrigins.Add("https://client.com");
                foreach (var item in socketAllowOrigins)
                {
                    webSocketOptions.AllowedOrigins.Add(item);
                }
              
            }
            app.UseWebSockets(webSocketOptions);
            app.UseMiddleware<WebsocketHandlerMiddleware>();
            app.UseMiddleware<RequestLogMiddleware>();

        }

        public static void InitMap(this IEndpointRouteBuilder app)
        {
            app.MapControllers();
            //.RequireAuthorization()  

            app.MapGet("/app/Test", () =>
            {

                return "AceProgram";
            });
        }
    }
}
